/**
 * 
 */
package com.yunyike.controller.category;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yunyike.constants.CommonConstant;
import com.yunyike.constants.RequestConstant;
import com.yunyike.constants.ResponseConstant;
import com.yunyike.controller.BaseController;
import com.yunyike.model.Category;
import com.yunyike.response.Response;
import com.yunyike.service.ICategoryService;
import com.yunyike.utils.mybatis.PageBean;

/**
 * @author zhengqiang.shi
 * 2016-4-8 下午2:45:39
 */
@Controller
@RequestMapping(value=RequestConstant.CATEGORY)
public class CategoryController extends BaseController{

	@Autowired
	private ICategoryService categoryService;
	
	private static final Logger log = LoggerFactory.getLogger(CategoryController.class);
	
	/**
	 * 分类列表 返回json数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.CATEGORY_LIST)
	public void categoriesPost(HttpServletResponse response,Integer pageSize,Integer page){
		
		// response object
		Response res = new Response();
		
		PageBean<String> pageBean = new PageBean<String>();
		pageBean.setPage(page == null?1:page);
		if(pageSize != null){
			pageBean.setPageSize(pageSize);
		}
		
		List<Category> categories;
		try {
			categories = categoryService.findCategoryByPage(pageBean);
		} catch (Exception e) {
			res.getResult().setCode(CommonConstant.NUMBER__3);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__3));
			returnDatas(res, response);
			return;
		}
		
		if(categories == null || categories.isEmpty()){
			log.info("no categories find.");
			res.getResult().setCode(CommonConstant.NUMBER__2);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__2));
			returnDatas(res, response);
			return;
		}
		
		res.setData(categories);
		returnDatas(res, response);
		return;
	}
}
